package com.api.gbhg.lbj.api;

import com.alibaba.fastjson.JSON;
import com.weaver.general.BaseBean;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.workflow.workflow.WorkflowVersion;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import static com.api.gbhg.lbj.tool.Tools.getsfkzsydwbxed;
import static com.api.gbhg.lbj.tool.Tools.getsubcompanyId;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO 校验预算项目使用额度
 * @date 2021/4/29 15:48
 */
@Path("/gbhg/user")
public class UserQuotaCheckApi {
    private static final Logger logger = LoggerFactory.getLogger("gbhg");
    @POST
    @Path("/quotaCheck")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String quotaCheck(Map<String, String>[] param){
        logger.info("进入预算项目使用单位额度校验--quotaCheck");
        RecordSet rs1=new RecordSet();
        Map<String,Object> map=new HashMap<>();
        for (int i=0;i<param.length;i++) {
            BigDecimal money = new BigDecimal(param[i].get("money"));//申请报销金额
            String lcjlid = param[i].get("ysxm");//预算项目
            String bxr = param[i].get("bxr");//报销人
            String sydw = getsubcompanyId(bxr);
            Integer detaileId = Integer.valueOf(param[i].get("detaileId"));//代表当前明细行
            RecordSet rs = getsfkzsydwbxed(lcjlid);
            rs.next();
            String sfkzsydwbxed = rs.getString("sfkzsydwbxed");
            String lcid = rs.getString("lcid");
            BaseBean baseBean=new BaseBean();
            String BZ18Workflowid = WorkflowVersion.getActiveVersionWFID(baseBean.getPropValue("FormationWorkflowid", "BZ18Workflowid"));//读取配置文件
            logger.info("是否控制使用单位报销额度:"+sfkzsydwbxed+",workflowid:"+lcid+",BZ18Workflowid:"+BZ18Workflowid);
            if (StringUtils.isNotBlank(sfkzsydwbxed)&&sfkzsydwbxed.equals("1")&&!lcid.equals(BZ18Workflowid)) {
                String sql = "select syed from uf_ysxmsydwfped where xmbzlc=" + lcjlid + " and sydw=" + sydw;
                rs1.executeQuery(sql);
                while (rs1.next()) {
                    BigDecimal syed = new BigDecimal(rs1.getString("syed"));
                    if (money.compareTo(syed) == 1) {
                        map.put("code", "300");
                        map.put("msg", "明细行" + detaileId + 1 + ",预算项目使用单位分配额度不足！");
                    } else {
                        map.put("code", "200");
                    }
                }
            }else {
                map.put("code", "200");
            }
        }
        return JSON.toJSONString(map);
    }
}
